package com.platform.modules.order;

import com.platform.common.exception.BusinessException;
import com.platform.common.utils.JedisUtil;
import com.platform.common.utils.StringUtils;
import com.platform.common.utils.ValidUtils;
import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import com.platform.modules.mall.entity.MallUserEntity;
import com.platform.modules.mall.service.*;
import com.platform.modules.sys.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Component
public class BaseBuildSelect implements IOrderBuildSelect, IOrderInitParams<BaseInitParams> {

    @Resource
    private MallGoodsService goodsService;
    @Resource
    private MallGoodsToActivityService goodsToActivityService;
    @Resource
    private MallActivityService activityService;
    @Resource
    private MallOrderService orderService;
    @Resource
    private MallOrderGoodsService orderGoodsService;
    @Resource
    private MallWhitelistService whitelistService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private JedisUtil jedisUtil;

    void paramsVerify(BaseInitParams initParams) {
        ValidUtils.allNotNull(initParams.getLoginUser(), initParams.getParams().getNumber(), initParams.getParams().getGoodsId(), initParams.getParams().getFromType());
        MallGoodsEntity entity = goodsService.getById(initParams.getParams().getGoodsId());
        initParams.setEntity(entity);
        if (entity.getGoodsType() == null) {
            throw new BusinessException("商品参数配置有误");
        }
        if (entity.getIsLimited() > 0 && entity.getIsLimited() < initParams.getParams().getNumber()) {
            throw new BusinessException("超出限购数量！");
        }
        if (null == entity.getGoodsNumber() || entity.getGoodsNumber() < initParams.getParams().getNumber()) {
            throw new BusinessException("商品库存不足");
        }
    }

    void paramsInit(BaseInitParams initParams) {
        // 判断是否为活动商品
        List<MallGoodsToActivityEntity> goodsToActivityEntities = goodsToActivityService.lambdaQuery()
                .select(MallGoodsToActivityEntity::getActivityId, MallGoodsToActivityEntity::getId)
                .eq(MallGoodsToActivityEntity::getGoodsId, initParams.getParams().getGoodsId())
                .list();
        if (goodsToActivityEntities != null && !goodsToActivityEntities.isEmpty()) {
            initParams.getParams().setActivityId(goodsToActivityEntities.get(0).getActivityId());
            initParams.setIsActivity(StringUtils.isNotBlank(initParams.getParams().getActivityId()));
            if (!initParams.getIsActivity()) {
                throw new BusinessException("获取活动信息出错，请联系管理员！");
            }
        }

        if (initParams.getIsActivity()) {
            initParams.setActivityStatus(activityService.getActivityStatus(initParams.getParams().getActivityId(), initParams.getParams().getGoodsId()));
            if (!initParams.getActivityStatus().getIsStart()) {
                throw new BusinessException("不在活动时间！");
            }
        }

        initParams.setIsLlShops(initParams.getEntity().getThirdSysNumber().equals(MallGoodsEntity.SysNumber.LL.getValue()));
        initParams.setIsCard(initParams.getEntity().getGoodsType() == MallGoodsEntity.GoodsType.CARD.ordinal());
        initParams.setIsCardPack(initParams.getEntity().getGoodsType() == MallGoodsEntity.GoodsType.CARD_PACK.ordinal());
    }

    @Override
    public BaseInitParams getInitParams(ParamsEntity params, MallUserEntity loginUser) {
        BaseInitParams initParams = new BaseInitParams();
        initParams.setLoginUser(loginUser);
        initParams.setParams(params);
        this.paramsVerify(initParams);
        this.paramsInit(initParams);
        return initParams;
    }

    @Override
    public AbsOrderBuild selectOrderBuild(ParamsEntity params, MallUserEntity loginUser) {
        BaseInitParams initParams = this.getInitParams(params, loginUser);
        AbsOrderBuild<BaseInitParams> orderBuild = null;
        if (initParams.getIsActivity()) {
            switch (params.getActivityId()) {
                case "4d024eb0a23dd05e8b2f8694ee264475":
                case "41116574c78223526bade37c884ba9cd":
                    // 白名单活动商品
                    orderBuild = new WActivityOrderBuild(sysConfigService, jedisUtil, goodsToActivityService, whitelistService, orderGoodsService,goodsService);
                    break;
                default:
                    // 默认活动商品
                    orderBuild = new ActivityOrderBuild(sysConfigService, jedisUtil, goodsToActivityService,goodsService,orderGoodsService);
                    break;
            }
        }
//        if (initParams.getIsLlShops()) {
//            // 联联商品
//            orderBuild = new LLOrderBuild(llProductInfoService, llProductSetMealService, llOrderService, orderGoodsService);
//        }
        if (orderBuild == null) {
            throw new BusinessException("未知的订单类型！");
        }
        // 设置订单初始参数
        orderBuild.init(initParams);
        return orderBuild;
    }

}
